//
//  ZZWebViewController.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/18.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZWebViewController.h"
#import<WebKit/WebKit.h>

@interface ZZWebViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong)  WKWebView  *webView;
@property (nonatomic,strong) UIProgressView *progress;
@end

@implementation ZZWebViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //    WebViewConfiguration
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    
    configuration.preferences = preferences;
    
    WKUserContentController *userController = [[WKUserContentController alloc]init];

    configuration.userContentController = userController;
    
    //webView
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    _webView.autoresizesSubviews = NO;
    _webView.translatesAutoresizingMaskIntoConstraints = NO;
    //设置背景色
    _webView.opaque = NO;
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
    [self.view addSubview:_webView];
    [self adJustScrollView];
    [self addObserver];

}

- (void)adJustScrollView{

    for (UIView * views in [_webView subviews])
    {
        if ([views isKindOfClass:[UIScrollView class]])
        {
            //去掉水平方向的滑动条
            [(UIScrollView *)views setShowsHorizontalScrollIndicator:NO];
            //去掉垂直方向的滑动条
            [(UIScrollView *)views setShowsVerticalScrollIndicator:NO];
        }
    }
}
- (void)addObserver{
    //KVO实现获取title和进度条
    //1.注册观察者
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    [_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
}
//2.观察者接收通知
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //加载进度值
    if ([keyPath isEqualToString:@"estimatedProgress"])
    {
        if (object == _webView)
        {
            [self.progress setAlpha:1.0f];
            _webView.frame = CGRectMake(0, 2, KWidth, KHeight - 2-64);
            [self.progress setProgress:_webView.estimatedProgress animated:YES];
            if(_webView.estimatedProgress >= 1.0f)
            {
                [UIView animateWithDuration:0.5f
                                      delay:0.3f
                                    options:UIViewAnimationOptionCurveEaseOut
                                 animations:^
                 {
                     [self.progress setAlpha:0.0f];
                 }
                                 completion:^(BOOL finished)
                 {
                     [self.progress setProgress:0.0f animated:NO];
                     _webView.frame = CGRectMake(0, 0, KWidth, KHeight - 0-44);
                 }];
            }
        }
    }
    
    //网页title
    if ([keyPath isEqualToString:@"title"])
    {
        if (object == _webView)
        {
            self.navigationItem.title = _webView.title;
        }
    }

}

- (void)dealloc
{
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [_webView removeObserver:self forKeyPath:@"title"];
}

- (UIProgressView *)progress
{
    if (_progress == nil)
    {
        _progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 2)];
        _progress.tintColor = [UIColor colorWithRed:61.0/255.0 green:163.0/255.0 blue:225.0/255.0 alpha:0.5];
        _progress.tintColor = [UIColor greenColor];
        //        _progress.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_progress];
    }
    return _progress;
}

@end
